#!/usr/bin/gawk -f
# hack to extract GMP-style documentation from source
# extremely fragile, requires function definition/declaration
# to end with ")\n" or ");" (e.g. no whitespace allowed)
# and does not play well with function pointer parameters
/^\/\*@/ {
    outfile = "gen/" gensub(".*manual ", "", 1) ".xml"
    print "Writing to " outfile
    n = 0
    getline
    while ($0 != "*/") {
	a[n] = $0
	n++
	getline
    }

    print "<varlistentry><term>" > outfile
    print "<function>" > outfile
#    do {
#	getline
#	print
#    } while (!match($0, ";") && !match($0, "{"))
    getline
    sub("static inline ", "")
    s = gensub("(\\w*)\\(", " <command>\\1</command>(", 1)
    s = gensub("\\((.*$)", "(<parameter>\\1", 1, s)
    gsub(",", "</parameter><parameter>", s)
    gsub("_ptr", "_t", s)
    while (!match(s, ");") && !match(s, ")$")) {
	print s > outfile
	getline
	s = $0
	gsub(",", "</parameter><parameter>", s)
	gsub("_ptr", "_t", s)
    }
    s = gensub("(.*)\\)", "\\1</parameter>)", 1, s)
    gsub("_ptr", "_t", s)
    gsub(";", "", s);
    print s > outfile
    print "</function>" > outfile
    print "</term>" > outfile
    
    print "<listitem><para>" > outfile
    for(i=0; i<n; i++) {
	a[i] = gensub("''([^']*)''", "<parameter>\\1</parameter>", "g", a[i])
	print a[i] > outfile
    }
    print "</para></listitem>" > outfile
    print "</varlistentry>\n" > outfile
}
